# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render_to_response,redirect
from weibopy.auth import OAuthHandler
from weibo.models import Sina_Request_Token,Sina_Access_Token
from myuser.usercheck import getUser
from settings import URL_ROOT,LONGCOOKIETIME,SHORTCOOKIETIME

callbackurl = URL_ROOT+"/weibo/saveauth"

def connect(request):
    oah = OAuthHandler("1330786948","94fa907c07c9d6915927f48149f431d8",callbackurl)
    
    theurl = oah.get_authorization_url()
    
    rt = Sina_Request_Token()
    rt.thekey = oah.request_token.key
    rt.secret = oah.request_token.secret
    rt.save()

    return redirect(theurl)

def saveauth(request):
    """
    need to add the function of setting cookie with render_to_response
    """
    oah = OAuthHandler("1330786948","94fa907c07c9d6915927f48149f431d8")
    ot = request.GET['oauth_token']
    os = Sina_Request_Token.all().filter("thekey = ",ot)[0].secret
    ov = request.GET['oauth_verifier']
    
    oah.set_request_token(ot,os)
    access_token = oah.get_access_token(ov)
    uid = str(oah.get_id())
    username = oah.get_username()

    #here do like the douban
    curuser = getUser(request)
    if Sina_Access_Token.all().filter("sinaid =",uid).filter("userref != ",None).count() != 0:
        sat = Sina_Access_Token.all().filter("sinaid =",uid)[0]
        if sat.userref != None:
            dic = {}
            if curuser == None or sat.userref == curuser:
                #the == may have some problem
                dic['theurl'] = URL_ROOT
                dic['thedesp'] = "login with douban success"
                hr = render_to_response("tools/redirect.html",dic)
                hr.set_cookie("key",sat.userref.key(),LONGCOOKIETIME)
                hr.set_cookie("pwd",sat.userref.pwd,LONGCOOKIETIME)
                return hr
            else:
                dic['theurl']= URL_ROOT+"/basicuser/connectstatus"
                dic['thedesp'] = "this sina account is alread linked to another"
                hr =  render_to_response("tools/redirect.html",dic)
                hr.set_cookie("key",sat.userref.key(),LONGCOOKIETIME)
                hr.set_cookie("pwd",sat.userref.pwd,LONGCOOKIETIME)
                return hr

    scount = Sina_Access_Token.all().filter("sinaid = ",uid).count()
    if scount == 0:
        #create new structure
        at = Sina_Access_Token()
        at.thekey = access_token.key
        at.secret = access_token.secret
        at.sinaid = uid#username
        at.sinaname = username
        at.put()
    elif scount == 1:
        at = Sina_Access_Token.all().filter("sinaid =",uid)[0]
        at.thekey = access_token.key
        at.secret = access_token.secret
        at.put()
    else:
        raise Excpetion("a sina id has more than an item")

    #here should redirect to the connect to curuser page
    dic = {}
    dic['username'] = username
    dic['sinaid'] = uid
    dic['sinaaccesstoken'] = access_token.key
    hr = render_to_response("myuser/conExistUser.html",dic)
    #hr.set_cookie("sinaaccesstoken",access_token.key,864000)
    #hr.set_cookie("suid",oah.get_username(),864000)

    return hr
